package com.bishe.tax.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bishe.tax.entity.IndexPicture;
import com.bishe.tax.entity.TaxNews;
import com.bishe.tax.service.IndexPictureService;
import com.bishe.tax.service.TaxNewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;

/**
 * @Auther: xhl
 * @Date: 2019/6/10
 * @Description:
 */
@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    private TaxNewsService newsService;
    @Autowired
    private IndexPictureService pictureService;

    @GetMapping("/")
    public String index(ModelMap mmp) {
        TaxNews news = new TaxNews();
        news.setNewsType("0");
        PageHelper.startPage(1, 9);
        List<TaxNews> newsList = newsService.selectNewsList(news);
        PageInfo<TaxNews> page = new PageInfo<>(newsList);
        mmp.put("newsList", page.getList());

        PageHelper.startPage(1, 3);
        List<IndexPicture> picList = pictureService.selectPictureList(new IndexPicture());
        PageInfo<IndexPicture> picPage = new PageInfo<>(picList);
        mmp.put("photosList", picPage.getList());

        news.setNewsType("1");
        PageHelper.startPage(1, 5);
        newsList = newsService.selectNewsList(news);
        page = new PageInfo<>(newsList);
        mmp.put("TaxList", page.getList());
        return "web/main";
    }

    @GetMapping("/about")
    public String about(ModelMap mmp) {
        return "web/about";
    }

    @GetMapping("/news/{id}")
    public String news(@PathVariable("id") String id, ModelMap mmp) {
        TaxNews news = newsService.selectNewsById(Integer.valueOf(id));
        mmp.put("news", news);
        PageHelper.startPage(1, 5);
        List<TaxNews> newsList = newsService.selectNewsListRandom();
        PageInfo<TaxNews> page = new PageInfo<>(newsList);
        mmp.put("newsList", page.getList());
        return "web/news";
    }

    @GetMapping("/newsList")
    public String newsList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, ModelMap mmp) {
        PageHelper.startPage(pageNum, 5);
        List<TaxNews> newsList = newsService.selectNewsList(new TaxNews());
        PageInfo<TaxNews> page = new PageInfo<>(newsList);
        mmp.put("newsList", page.getList());
        mmp.put("page", page);
        return "web/newsList";
    }

    
    @RequestMapping("/images/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 设置请求头为输出图片类型
        CaptchaUtil.setHeader(response); 
        // 三个参数分别为宽、高、位数
        GifCaptcha gifCaptcha = new GifCaptcha(300, 48, 3);  
        // 设置类型，纯数字、纯字母、字母数字混合
        gifCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER); 
        // 验证码存入session
        request.getSession().setAttribute("captcha", gifCaptcha.text().toLowerCase()); 
        // 输出图片流
        gifCaptcha.out(response.getOutputStream());
    }
    
    
}
